package com.polaris.live.ui.me.beans

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.common.constant.BizEventConst
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.event.EventMessage
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ImmersionBarBuilder
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityMyWithdrawBinding
import com.polaris.live.resp.back_resp.WithdrawAccountInfoVo
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.utils.route.RouterUtils
import com.polaris.live.viewmodel.GlobalViewModelProxy
import com.polaris.live.viewmodel.MyBeansViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 提现页面
 *
 * @author Created by 长空 on 2023/10/26/024 09:42
 */
class MyWithdrawActivity : BaseVbActivity<MyBeansViewModel, ActivityMyWithdrawBinding>() {

    private var mWithdrawAccountInfoVo: WithdrawAccountInfoVo? = null

    private var mIsLose = true

    override fun ImmersionBarBuilder.buildImmersionBar() {
        setTitleBar(mBinding.bvTitle)
    }

    override fun initView(savedInstanceState: Bundle?) {
        EventBus.getDefault().register(this)

        showHideStatus(View.GONE)

        initObsever()
        initListener()
    }

    private fun showHideStatus(status: Int) {
        mBinding.spMoney.visibility = status
        mBinding.btnGetMoney.visibility = status
        mBinding.tvAttention.visibility = status
        mBinding.tvRule.visibility = status
    }

    private fun initListener() {
        mBinding.viewAddWithdraw.setOnSingleClickListener {
            mWithdrawAccountInfoVo?.let { it1 ->
                Router.instance.setPath(RoutePath.MYWITHDRAW_DETAIL_ACTIVITY)
                    .withAny(CommonConst.Withdraw.Withdraw_DATA, it1).navigation(this)
            } ?: run {
                Router.instance.setPath(RoutePath.MYWITHDRAW_DETAIL_ACTIVITY).navigation(this)
            }
        }
        mBinding.csWithdrawDetail.setOnSingleClickListener {
            mWithdrawAccountInfoVo?.let { it1 ->
                Router.instance.setPath(RoutePath.MYWITHDRAW_DETAIL_ACTIVITY)
                    .withAny(CommonConst.Withdraw.Withdraw_DATA, it1).navigation(this)
            } ?: run {
                Router.instance.setPath(RoutePath.MYWITHDRAW_DETAIL_ACTIVITY).navigation(this)
            }
        }
        mBinding.btnGetMoney.setOnSingleClickListener {
            if (mIsLose) {
                ToastGravity.showGravity(getString(string.service_provider_is_invalid))
                return@setOnSingleClickListener
            }
            mWithdrawAccountInfoVo?.let { it1 -> mViewModel.withdrawV2(it1) }
        }
    }

    private fun initObsever() {
        mViewModel.mLastWithdrawLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mBinding.tvRule.text = it.withdrawRuleDesc
                initData(it.withdrawAccountInfoVo)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
        mViewModel.mWithdrawLiveData.observe(this) { resultState ->
            parseState(resultState, {
                Router.builder(RoutePath.WITHDRAW_SUCCESS_ACTIVITY)
                    .withString("price", mWithdrawAccountInfoVo?.amount.toString())
                    .navigation(this)
                finish()
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }
        GlobalViewModelProxy.getCustomerService(mViewModel) { resultState ->
            parseState(resultState, { resp ->
                if (resp.display.toBoolean()) {
                    mBinding.bvTitle.setRightResource(R.drawable.ic_kefu)
                    mBinding.bvTitle.setOnRightClientListener {
                        resp.url?.let { RouterUtils.routerCustomerService(ImageUtils.imgPath(it)) }
                    }
                }
            })
        }
    }

    private fun initData(data: WithdrawAccountInfoVo?) {
        data?.let {
            mWithdrawAccountInfoVo = it
            showHideStatus(View.VISIBLE)
            mBinding.tvWithdraw.text =
                getString(string.Withdrawable, it.withdrawAmount)
            mBinding.tvIncomeNum.text = it.gainCoin.toString()
            mBinding.tvRewardNum.text = it.sendGainCoin.toString()
            mBinding.tvOmmission.text =
                getString(string.commission_charge, it.fee)

            mBinding.tvGetMoney.text = StringUtils.getString(string.Get_Money, it.amount)

            mBinding.groupAdd.visibility = View.GONE
            mBinding.csWithdrawDetail.visibility = View.VISIBLE
            mBinding.tvBank.text = it.channelName
            mBinding.tvFreeCard.text = it.accountNum
            if (it.status == 0) {
                mIsLose = true
                mBinding.tvLose.visibility = View.VISIBLE
                mBinding.btnGetMoney.alpha = 0.3f
                mBinding.btnGetMoney.isEnabled = false
            } else {
                mIsLose = false
                mBinding.tvLose.visibility = View.GONE
                val floatAmount = it.amount.toFloatOrNull()
                if (floatAmount == null || floatAmount <= 0) {
                    mBinding.btnGetMoney.alpha = 0.3f
                    mBinding.btnGetMoney.isEnabled = false
                } else {
                    mBinding.btnGetMoney.alpha = 1f
                    mBinding.btnGetMoney.isEnabled = true
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: EventMessage) {
        when (event.from) {
            BizEventConst.WITHDRAW_DETAIL -> {//提现
                initData(event.data as WithdrawAccountInfoVo)
            }

            BizEventConst.WITHDRAW_DETAIL_DELETE -> {//删除提现账号
                showHideStatus(View.GONE)
                mWithdrawAccountInfoVo = null
                mBinding.btnGetMoney.alpha = 0.3f
                mBinding.btnGetMoney.isEnabled = false
                mBinding.groupAdd.visibility = View.VISIBLE
                mBinding.csWithdrawDetail.visibility = View.GONE
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.lastWithdraw()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

}